import { _decorator, Component, director, EventTouch, Label, log, Node, PhysicsSystem2D, ProgressBar, v3, Vec3 } from 'cc';
import { GameManager } from './GameManager';
import { Money } from './Money';
import { EPOCH, LV, TASKTYPE } from '../AshFramework/Datas/GameConstant';
import { GameUtil } from './GameUtil';
import { WeaponBuyer } from './WeaponBuyer';
import { CameraController } from './CameraController';
import { Chariot } from './Chariot';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { MapController } from './MapController';
const { ccclass, property } = _decorator;

const ICON_DIS = 740;
const ICON_STARTPOS: Vec3 = v3(-370, 0, 0);

export enum CHARIOTSPEED {
    PAUSE = 0,
    RESUME = 3,
}

@ccclass('BattleManager')
export class BattleManager extends Component {
    private static _instance: BattleManager = null;
    public static get Instance(): BattleManager {
        if (!BattleManager._instance) {
            BattleManager._instance = new BattleManager();
        }
        return BattleManager._instance;
    }
    @property(Node)
    SetWindow: Node = null;

    @property(Node)
    PauseWindow: Node = null;

    @property(Node)
    GoldNode: Node = null;

    @property(Node)
    MasonryNode: Node = null;

    @property(Node)
    EpochNode: Node = null;

    @property(Node)
    LvNode: Node = null;

    @property(Label)
    BattleValue: Label = null;//战斗力

    @property(Node)
    AwardGoldValue: Node = null;//获得的金币

    @property(Label)
    ResidueLabel: Label = null;

    @property(ProgressBar)
    EnergyProgress: ProgressBar = null;

    @property(Label)
    EnergyLabel: Label = null;

    @property({ type: [Node] })
    WeaponBtn: Node[] = [];

    @property(ProgressBar)
    LVProgress: ProgressBar = null;

    @property(Node)
    ICONNode: Node = null;

    @property(Node)
    UIGoldNode: Node = null

    private _isPause: boolean = false;

    private _battleValue: number = 0;
    private _awardGold: number = 0;
    private _energySpeed: number = 0
    private _multiple: number = 1;
    private _arrWeaponBtn: Array<number> = new Array();
    public curEnergy: number = 0;

    protected onLoad(): void {
        BattleManager._instance = this;

        PhysicsSystem2D.instance.enable = true;
        // PhysicsSystem2D.instance.debugDrawFlags = 1;

        this._energySpeed = GameManager.Instance.userData.energy * (1 + GameManager.Instance.userData.propertyAmplification.FOOD / 100);//属性赋值
    }

    protected start(): void {
        this.showUI();
        this.showWeaponBtn();
    }

    protected update(dt: number): void {
        if (!this._isPause) {
            this.productionEnergy(dt);
        }
    }

    gamePause() {
        this._isPause = true;
        Chariot.Instance.dirX = CHARIOTSPEED.PAUSE;
        EventManager.Scene.emit(MyEvent.MONSTER_DIRX, 0);
    }

    gameResume() {
        this._isPause = false;
        Chariot.Instance.dirX = CHARIOTSPEED.RESUME;
        EventManager.Scene.emit(MyEvent.MONSTER_DIRX, -10);
    }

    gameBackBtn() {
        console.error(`退出游戏！`);
    }

    setBtn() {
        this.gamePause();
        this.SetWindow.active = true;
    }

    pauseBtn() {
        this.gamePause();
        this.PauseWindow.active = true;
    }

    speedUpBtn(event: EventTouch) {
        this._multiple = 2;
        event.currentTarget.active = false;
    }

    awardBtn(event: EventTouch) {
        this.curEnergy += 99;
        event.currentTarget.active = false;
        this.updateEnergy();
    }

    endBtn() {
        director.loadScene("Menu");
    }

    //更新关卡的进度条
    updateLVProgress(progress: number) {
        this.LVProgress.progress = progress;
        this.ICONNode.position = ICON_STARTPOS.clone().add3f(progress * ICON_DIS, 0, 0);
    }

    showUI() {
        this.GoldNode.getComponent(Money).setMoney(GameManager.Instance.userData.gold);
        this.MasonryNode.getComponent(Money).setMoney(GameManager.Instance.userData.masonry);
        this.EpochNode.getComponent(Label).string = GameManager.Instance.userData.epoch;
        this.LvNode.getComponent(Label).string = GameManager.Instance.userData.lv;
    }

    addBattleValue(value: number) {
        this._battleValue += value;
        this.BattleValue.string = GameUtil.formatNumber(this._battleValue);
    }

    addAwardGold(gold: number) {
        this._awardGold += gold;
        this.AwardGoldValue.getComponent(Money).addMoney(gold);
        // this.AwardGoldValue = GameUtil.formatNumber(this._awardGold);
        if (GameManager.Instance.userData.task.type === TASKTYPE.GOLD) {
            GameManager.Instance.userData.task.curProgress += gold;
        }
    }

    //生产能量
    productionEnergy(dt: number) {
        let curProgress = this.EnergyProgress.progress + dt * this._energySpeed * this._multiple;
        if (curProgress >= 1) {
            this.curEnergy++;
            curProgress = 0;
            this.updateEnergy();
            if (GameManager.Instance.userData.task.type === TASKTYPE.FOOD) {
                GameManager.Instance.userData.task.curProgress++;
            }
        }
        this.EnergyProgress.progress = curProgress;
    }
    //显示更新剩余的能量
    updateEnergy() {
        this.EnergyLabel.string = GameUtil.formatNumber(this.curEnergy);
    }

    //武器按钮的展示
    showWeaponBtn() {
        let index = 0;
        let arrWeapon = GameManager.Instance.userData.weapon;
        for (let i = 0; i < arrWeapon.length; i++) {
            if (arrWeapon[i] != 0) {
                this._arrWeaponBtn.push(arrWeapon[i]);
            }
        }
        for (let i = 0; i < this._arrWeaponBtn.length; i++) {
            this.WeaponBtn[index].active = true;
            this.WeaponBtn[index].getComponent(WeaponBuyer).init(this._arrWeaponBtn[i], index + 1);
            index++;
        }
    }

    getUIGoldWorldPos() {
        return this.UIGoldNode.getWorldPosition().clone();
    }

    protected onEnable(): void {

    }
}


